package com.pearl.boot.web.config;

import com.fasterxml.jackson.databind.JsonSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * Spring MVC 配置类
 *
 * @author TangDan
 * @version 1.0
 * @since 2022/9/5
 */
public class PearlWebMvcConfiguration implements WebMvcConfigurer {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        Map<Class<?>, JsonSerializer<?>> map = new HashMap<>();
        map.put(BigInteger.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        map.put(BigDecimal.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        map.put(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        map.put(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        return builder -> builder.serializersByType(map);
    }
}
